﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Presentation.Web.Admin.Controllers
{
    [HandleError]
    public class LoginController : Controller
    {
        Infrastructure.Crosscutting.Authorize.IUserService userService;

        Infrastructure.Crosscutting.Authorize.IUserRoleService userRoleService;

        Infrastructure.Crosscutting.Authorize.IRoleService roleService;

        Infrastructure.Crosscutting.Caches.ICacheService cacheService;

        public LoginController(Infrastructure.Crosscutting.Authorize.IUserService user,
            Infrastructure.Crosscutting.Authorize.IUserRoleService userRole,
            Infrastructure.Crosscutting.Authorize.IRoleService role,
            Infrastructure.Crosscutting.Caches.ICacheService cache)
        {
            if (user == null)
            {
                throw new ArgumentNullException("userService is null");
            }
            if (userRole == null)
            {
                throw new ArgumentNullException("userRoleService is null");
            }
            if (role == null)
            {
                throw new ArgumentNullException("roleService is null");
            }
            if (cache == null)
            {
                throw new ArgumentNullException("cacheService is null");
            }
            userService = user;
            userRoleService = userRole;
            roleService = role;
            cacheService = cache;
        }

        public ViewResult Index()
        {
            var model = new Infrastructure.Crosscutting.Authorize.Dto.LoginDto();
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(Infrastructure.Crosscutting.Authorize.Dto.LoginDto dto)
        {
            if (!string.IsNullOrEmpty(dto.UserName) && !string.IsNullOrEmpty(dto.Password))
            {
                var userModel = userService.GetUserModelByUserNameAndPwd(dto.UserName, dto.Password);
                if (userModel != null && userModel.Id > 0)
                {
                    Infrastructure.Crosscutting.Comm.Service.SessionService.Add(Infrastructure.Crosscutting.Comm.Service.ConstService.SessionNameCurrentUser,
                        userModel, Infrastructure.Crosscutting.Comm.Service.ConstService.SessionExpires);
                    //Session[Infrastructure.Crosscutting.Comm.Service.ConstService.SessionNameCurrentUser] = userModel;
                    var rediUrl = (from user in userService.GetList()
                                   join userRole in userRoleService.GetList() on user.Id equals userRole.UserId
                                   join role in roleService.GetList() on userRole.RoleId equals role.Id
                                   where user.Id == userModel.Id
                                   select role.DefaultUrl).FirstOrDefault();
                    if (!string.IsNullOrEmpty(rediUrl))
                    {
                        return Redirect(rediUrl);
                    }
                    else
                    {
                        return Redirect("/");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "用户名或密码不正确");
                }
            }
            else
            {
                ModelState.AddModelError("", "用户名或密码错误");
            }
            return View(dto);
        }

        public RedirectResult Off()
        {
            var user = Session[Infrastructure.Crosscutting.Comm.Service.ConstService.SessionNameCurrentUser] as
             Domain.Entity.User;
            if (user != null)
            {
                Session[Infrastructure.Crosscutting.Comm.Service.ConstService.SessionNameCurrentUser] = null;
                Session.Abandon();
                Session.Clear();
                var userGuid = user.Guid;
                cacheService.Remove(typeof(List<Domain.Entity.UserRole>).ToString() + userGuid);
                cacheService.Remove(typeof(List<Domain.Entity.MvcAction>).ToString() + userGuid);
                cacheService.Remove(typeof(List<Domain.Entity.MvcController>).ToString() + userGuid);
                cacheService.Remove(typeof(List<Domain.Entity.RoleMenu>).ToString() + userGuid);
            }
            return Redirect("/login/");
        }
    }
}
